﻿/// * Write a program that reads a number N and generates and prints all the permutations of the numbers [1 … N]. 
/// Example:	n = 3  
/// {1, 2, 3}, +
/// {1, 3, 2}, +
/// {2, 1, 3}, +
/// {2, 3, 1}, +
/// {3, 1, 2}, +
/// {3, 2, 1}  +

using System;

class zad19
{
    static void Main()
    {
        int n = 5;
        int[] arr = new int[n];

        for (int i = 1; i <= n; i++)
        {
            arr[i - 1] = i;
        }

        PrintPermute(arr, 0, n-1);
    }

    static void PrintPermute(int[] array, int current, int length)
    {
        if (current == length)
        {
            for (int i = 0; i <= length; i++)
            {
                Console.Write(array[i] + " ");
            }
            Console.WriteLine();
        }
        else
        {
            for (int i = current; i <= length; i++)
            {
                swapTwoPositions(array, i, current);
                PrintPermute(array, current + 1, length);
                swapTwoPositions(array, i, current);
            }
        }
    }

    

    private static void swapTwoPositions(int[] arr, int firstPos, int secondPos)
    {
        if (firstPos != secondPos)
        {
            int temp = arr[firstPos];
            arr[firstPos] = arr[secondPos];
            arr[secondPos] = temp; 
        }
    }
}

